package com.zhiziai.aiproxy.service;

import com.google.common.base.Preconditions;
import com.zhiziai.aiproxy.utils.OpenAIUtil;
import com.zhiziai.aiproxy.utils.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * @author: liuxinyu06
 * @description:
 * @create: 2022-04-07
 **/
@Slf4j
@Service
public class OpenAIServiceImpl {

    public OpenAIUtil.Msg search(String question) throws Exception {
        return OpenAIUtil.chatgpt(question);
    }

    public OpenAIUtil.Msg encryptChat(String question, String sign) throws Exception {
        Preconditions.checkArgument(StringUtils.isNotEmpty(question) && StringUtils.isNotEmpty(sign),
                "参数错误");
        String calSign = SignUtil.buildSign(question);
        Preconditions.checkArgument(StringUtils.isNotEmpty(calSign) && calSign.equals(sign), "auth error");
        return OpenAIUtil.chatgpt(question);
    }

    public List<String> encryptImgGen(String prompt, Integer genNum, String resulotionRate, String sign) throws Exception {
        Preconditions.checkArgument(StringUtils.isNotEmpty(prompt) && genNum != null
                && StringUtils.isNotEmpty(resulotionRate) && StringUtils.isNotEmpty(sign), "参数错误");
        String calSign = SignUtil.buildSign(prompt, genNum + "", resulotionRate);
        Preconditions.checkArgument(StringUtils.isNotEmpty(calSign) && calSign.equals(sign), "auth error");
        return OpenAIUtil.dalleGen(prompt, genNum, resulotionRate);
    }

    public List<String> encryptImgEdit(String prompt, Integer genNum, String resulotionRate, File image, File mask, String sign) throws Exception {
        Preconditions.checkArgument(StringUtils.isNotEmpty(prompt) && genNum != null
                && StringUtils.isNotEmpty(resulotionRate) && image != null && StringUtils.isNotEmpty(sign), "参数错误");
        String calSign = SignUtil.buildSign(prompt, genNum + "", resulotionRate);
        Preconditions.checkArgument(StringUtils.isNotEmpty(calSign) && calSign.equals(sign), "auth error");
        return OpenAIUtil.dalleEdit(prompt, genNum, image, mask, resulotionRate);
    }

    public String audioTranscriptionsEncrypt(MultipartFile file, String prompt, String language, String temperature, String randomSalt, String sign) throws Exception {
        Preconditions.checkArgument(file != null && StringUtils.isNotEmpty(randomSalt) && StringUtils.isNotEmpty(sign), "参数错误");
        String calSign = SignUtil.buildSign(prompt, language, randomSalt);
        Preconditions.checkArgument(StringUtils.isNotEmpty(calSign) && calSign.equals(sign), "auth error");
        language = StringUtils.isNotEmpty(language) ? language : "zh";
        temperature = StringUtils.isNotEmpty(temperature) ? temperature : "0.6";
        return OpenAIUtil.audioTranscriptionsEncrypt(file, language, prompt, temperature);
    }
}
